Page({
  data: {
    isDarkMode: false,
    hue: 45,
    saturation: 100,
    lightness: 50,
    ambilightBrightness: 100,
    ambilightColor: 'hsl(45,100%,50%)',
    ambilightBg: '#FFB300',
    colorMatrix: [],
    // 呼吸灯相关
    breathHue: 0,
    breathSaturation: 100,
    breathLightness: 50,
    breathBrightness: 100,
    breathColor: 'hsl(0,100%,50%)',
    discoSpeed: 60,
    discoBrightness: 100,
    discoColor: '#7f5fff',
    discoTimer: null,
    discoColors: ['', ''],
    discoAllColors: false
  },
  onLoad() {
    // 生成色环色块矩阵（如6行12列=72色）
    const rows = 6, cols = 12;
    let matrix = [];
    for (let i = 0; i < rows; i++) {
      let row = [];
      for (let j = 0; j < cols; j++) {
        // HSL色环采样
        const h = Math.round((j / cols) * 360);
        const s = 80;
        const l = 50 - (i - rows / 2) * 8; // 不同行亮度
        row.push(`hsl(${h},${s}%,${l}%)`);
      }
      matrix.push(row);
    }
    this.setData({ colorMatrix: matrix });
  },
  onShow() {
    const isDark = wx.getStorageSync('isDarkMode');
    this.setData({
      isDarkMode: typeof isDark === 'boolean' ? isDark : (getApp().globalData.isDarkMode || false)
    });
  },
  chooseAmbilightColor(e) {
    const color = e.currentTarget.dataset.color;
    this.setData({
      ambilightColor: color,
      ambilightBg: color
    });
  },
  onColorChange(e) {
    this.setData({
      ambilightColor: e.detail.color
    });
  },
  changeAmbilightBrightness(e) {
    this.setData({ ambilightBrightness: e.detail.value });
  },
  onHueChange(e) {
    this.setData({ hue: e.detail.value }, this.updateColor);
  },
  onSaturationChange(e) {
    this.setData({ saturation: e.detail.value }, this.updateColor);
  },
  onLightnessChange(e) {
    this.setData({ lightness: e.detail.value }, this.updateColor);
  },
  updateColor() {
    const { hue, saturation, lightness } = this.data;
    const color = `hsl(${hue},${saturation}%,${lightness}%)`;
    this.setData({ ambilightColor: color });
  },
  onHueChanging(e) {
    this.setData({ hue: e.detail.value }, this.updateColor);
  },
  onSaturationChanging(e) {
    this.setData({ saturation: e.detail.value }, this.updateColor);
  },
  onLightnessChanging(e) {
    this.setData({ lightness: e.detail.value }, this.updateColor);
  },
  onBrightnessChanging(e) {
    this.setData({ ambilightBrightness: e.detail.value });
  },
  goToFullscreen() {
    const { ambilightColor, ambilightBrightness } = this.data;
    wx.navigateTo({
      url: `/pages/ambilight_fullscreen/ambilight_fullscreen?color=${encodeURIComponent(ambilightColor)}&brightness=${ambilightBrightness}`
    });
  },
  onBreathHueChange(e) { this.setData({ breathHue: e.detail.value }, this.updateBreathColor); },
  onBreathSaturationChange(e) { this.setData({ breathSaturation: e.detail.value }, this.updateBreathColor); },
  onBreathLightnessChange(e) { this.setData({ breathLightness: e.detail.value }, this.updateBreathColor); },
  onBreathBrightnessChange(e) { this.setData({ breathBrightness: e.detail.value }); },
  onBreathHueChanging(e) { this.setData({ breathHue: e.detail.value }, this.updateBreathColor); },
  onBreathSaturationChanging(e) { this.setData({ breathSaturation: e.detail.value }, this.updateBreathColor); },
  onBreathLightnessChanging(e) { this.setData({ breathLightness: e.detail.value }, this.updateBreathColor); },
  onBreathBrightnessChanging(e) { this.setData({ breathBrightness: e.detail.value }); },
  updateBreathColor() {
    const { breathHue, breathSaturation, breathLightness } = this.data;
    const color = `hsl(${breathHue},${breathSaturation}%,${breathLightness}%)`;
    this.setData({ breathColor: color });
  },
  startBreathLight() {
    if (this.data.breathTimer) clearInterval(this.data.breathTimer);
    let t = 0;
    const { breathSaturation, breathLightness } = this.data;
    this.data.breathTimer = setInterval(() => {
      t += 1;
      const hue = (this.data.breathHue + t * 2) % 360;
      const color = `hsl(${hue},${breathSaturation}%,${breathLightness}%)`;
      this.setData({ breathColor: color });
    }, 60);
  },
  onUnload() {
    if (this.data.breathTimer) clearInterval(this.data.breathTimer);
  },
  goToBreathFullscreen() {
    const { breathHue, breathSaturation, breathLightness, breathBrightness } = this.data;
    wx.navigateTo({
      url: `/pages/breath_fullscreen/breath_fullscreen?hue=${breathHue}&saturation=${breathSaturation}&lightness=${breathLightness}&brightness=${breathBrightness}`
    });
  },
  onDiscoSpeedChange(e) { this.setData({ discoSpeed: e.detail.value }); },
  onDiscoSpeedChanging(e) { this.setData({ discoSpeed: e.detail.value }); },
  onDiscoBrightnessChange(e) { this.setData({ discoBrightness: e.detail.value }); },
  onDiscoBrightnessChanging(e) { this.setData({ discoBrightness: e.detail.value }); },
  addDiscoColor() {
    if (this.data.discoColors.length < 6) {
      this.setData({ discoColors: [...this.data.discoColors, ''] });
    }
  },
  removeDiscoColor(e) {
    const idx = e.currentTarget.dataset.index;
    const arr = this.data.discoColors.slice();
    arr.splice(idx, 1);
    this.setData({ discoColors: arr });
  },
  onDiscoColorInput(e) {
    const idx = e.currentTarget.dataset.index;
    const arr = this.data.discoColors.slice();
    arr[idx] = e.detail.value;
    this.setData({ discoColors: arr });
  },
  onDiscoAllColorsChange(e) {
    this.setData({ discoAllColors: e.detail.value.includes('all') });
  },
  goToDiscoFullscreen() {
    const { discoColors, discoAllColors } = this.data;
    if (discoAllColors) {
      wx.navigateTo({
        url: `/pages/disco_fullscreen/disco_fullscreen?colors=all&speed=120&brightness=100`
      });
      return;
    }
    if (discoColors.filter(c => c.trim()).length < 2) {
      wx.showToast({ title: '请至少输入两种颜色或勾选全色系', icon: 'none' });
      return;
    }
    let colorParam = discoColors.filter(c => c.trim()).join(',');
    wx.navigateTo({
      url: `/pages/disco_fullscreen/disco_fullscreen?colors=${encodeURIComponent(colorParam)}&speed=120&brightness=100`
    });
  }
}); 